const Base = require('./base.js');
const {uuid,arrayGroup} =require('../utils')
module.exports = class extends Base {

  async indexAction(){
        let res=await this.model('admin').indexQueryProject();
        this.assign({
          result: res,
        })
        return this.display();
      }


  //添加项目
  async projectAction() {
    let params= this.ctx.post();
    let param={...params,system_id:uuid(32),system_time:['exp', 'CURRENT_TIMESTAMP()']};

    let res=await this.model('admin').adminInsertProject(param);

    return (res)!==0?this.json({success:false,msg:'添加失败'}):this.json({success:true,msg:'添加成功'});
 
  }
    // 删除接口
    async delprojectAction(){
      let parmas=this.ctx.param('system_id')
      let res = await this.model('admin').adminDelete(parmas);
    return think.isEmpty(res)?this.json({success:false,result:res,msg:'删除失败'}):this.json({success:true,result:res,msg:'删除成功'});
    }
    // 修改接口
    async editAction(){
      let parmas= this.ctx.post();
      let res = await this.model('admin').adminEdit(parmas);
    return think.isEmpty(res)?this.json({success:false,result:res,msg:'修改失败'}):this.json({success:true,result:res,msg:'修改成功'});
    }


//二级

// 接口
async apiAction(){
  let parmas=this.ctx.param('system_id')
  let result = await this.model('admin').adminData(parmas);
  let groupResult = arrayGroup(result.result)
  this.assign({
    result: groupResult,
    project:result.project
  })
  return this.display('admin_addapi.html');
}




 // 展示接口
 async showAction() {
  let parmas = {
    api_id: this.ctx.param('api_id')
  }
  let res = await this.model('admin').indexFindById(parmas);

  return think.isEmpty(res) ? this.json({
    success: false,
    result: res,
    msg: '查找失败'
  }) : this.json({
    success: true,
    result: res,
    msg: '查找成功'
  });
}


  // 查找分类
  async groupAction() {
    let system_id=this.ctx.param('system_id')
    let res = await this.model('admin').indexFindGroup(system_id);
    return think.isEmpty(res) ? this.json({
      success: false,
      result: res,
      msg: '添加失败'
    }) : this.json({
      success: true,
      result: res,
      msg: '添加成功'
    });
  }
  // 添加接口
  async addAction() {
    let parmas = this.ctx.post();
    let param = { ...parmas,
      api_time: ['exp', 'CURRENT_TIMESTAMP()'],
      api_id: uuid(32)
    }
    // console.log(param);
    let res = await this.model('admin').addApiMethod(param);
    return (res) !== 0 ? this.json({
      success: false,
      msg: '添加失败'
    }) : this.json({
      success: true,
      msg: '添加成功'
    });
  }
  // 删除接口
  async deleteAction() {
    let parmas = {
      api_id: this.ctx.param('api_id')
    }
    let res = await this.model('admin').indexDeleteApi(parmas);
    // console.log(res);
    return think.isEmpty(res) ? this.json({
      success: false,
      result: res,
      msg: '删除失败'
    }) : this.json({
      success: true,
      result: res,
      msg: '删除成功'
    });
  }
  // 修改接口
  async updateAction() {
    let parmas = this.ctx.post();
    let res = await this.model('admin').indexUpdateApi(parmas);
    // console.log(res);
    return think.isEmpty(res) ? this.json({
      success: false,
      result: res,
      msg: '修改失败'
    }) : this.json({
      success: true,
      result: res,
      msg: '修改成功'
    });
  }

  //添加接口分类
 async addgroupAction() {
  let parmas = this.ctx.post();
  // console.log(parmas,'0000000')
  let param = { ...parmas,
    group_id: uuid(32)
  }
  let res = await this.model('admin').addGroupMethod(param);
  return (res) !== 0 ? this.json({
    success: false,
    msg: '添加失败'
  }) : this.json({
    success: true,
    msg: '添加成功'
  });
}

//删除接口分类
async delgroupAction() {
  let parmas = this.ctx.post();

  let res = await this.model('admin').deleteGroupMethod(parmas.group_id);
  return think.isEmpty(res) ? this.json({
    success: false,
    msg: '删除失败'
  }) : this.json({
    success: true,
    msg: '删除成功'
  });
}










};